<?php ob_start();?>
<html>
<?php 
	$root = $_SERVER['DOCUMENT_ROOT'];
 	include($root.'/standart/head.php');
 	include($root.'/standart/dateHeader.php');
 	include($root.'/standart/checkUser.php');
?>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Start Bootstrap - SB Admin Version 2.0 Demo</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../font-awesome/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../css/jquery.cleditor.css" />
    <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" media="screen"/>
	<link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/ajax.js"></script>
    <script type="text/javascript">
    function validate(evt) {
		var theEvent = evt || window.event;
	  	var key = theEvent.keyCode || theEvent.which;
	  	key = String.fromCharCode( key );
	  	var regex = /[0-9]|\./;
	  	if( !regex.test(key) ) {
	    	theEvent.returnValue = false;
	    	if(theEvent.preventDefault) theEvent.preventDefault();
	  	}
	}
	
	function onLoad(){
		parent.unloadPageX();
		$('.datepicker').datepicker();
		var table = $('#dataTables-example').dataTable({
            "language": {
                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
            }
        } );
        callMethod = "dateSet";
	}

	function dateSet(obj){
		if(obj.value == ''){
			return;
		}
		var menuId = obj.id.replace('applicable', '');
		if($('#price'+menuId).val() == ''){
			return;
		}
		if(!dateControl(obj.value)){
			obj.value = '';
			alert('Geçerli olduğu tarih bugünden büyük olmalıdır!');
			return;
		}
		if(document.getElementById('applicable'+menuId).value == document.getElementById('applicable'+menuId).getAttribute('default')
				&& document.getElementById('price'+menuId).value == document.getElementById('price'+menuId).getAttribute('default')){
			return;
		}
		url = 'setPrice.php?'
			+'menu_id='+menuId
			+'&price='+$('#price'+menuId).val()
			+'&applicable='+$('#applicable'+menuId).val();
		if(obj.getAttribute('priceid') >= 0){
			url = url+'&priceId='+obj.getAttribute('priceid');
		}
		ajx(url, 'endPrice');
	}
	
	function warningMessage() {
		pane = document.getElementById('invisContentView');
		if(pane.style.height == '0px'){
			pane.style.display = 'block';
			$("#invisContentView").animate({
			    height: "30px"
			}, 1000 );
			setTimeout(function(){warningMessage()}, 5000);
		}else{
			pane.style.display = 'block';
			$("#invisContentView").animate({
			    height: "0px"
			}, 1000 , function() {
				document.getElementById('invisContentView').style.display = 'none';
			});
		}
	}

	function changeFeature(obj){
		if(obj.id.indexOf('featured') >= 0){
			url = 'setFeature.php?'
				+'menu_id='+obj.id.replace('featured', '')
				+'&featured='+(obj.checked?'E':'H');
			ajx(url, 'endFeature');
		}else{
			url = 'setFeature.php?'
				+'menu_id='+obj.id.replace('promoted', '')
				+'&promoted='+(obj.checked?'E':'H');
			ajx(url, 'endFeature');
		}
	}

	function changePrice(obj){
		if(obj.value == ''){
			return;
		}
		var menuId = obj.id.replace('price', '');
		if($('#applicable'+menuId).val() == ''){
			alert('Geçerli olduğu tarih bilgisini ekleyiniz!');
			return;
		}
		if(!dateControl($('#applicable'+menuId).val())){
			$('#applicable'+menuId).val('');
			alert('Geçerli olduğu tarih bugünden büyük olmalıdır!');
			return;
		}
		if(document.getElementById('applicable'+menuId).value == document.getElementById('applicable'+menuId).getAttribute('default')
				&& document.getElementById('price'+menuId).value == document.getElementById('price'+menuId).getAttribute('default')){
			return;
		}
		url = 'setPrice.php?'
			+'menu_id='+menuId
			+'&price='+$('#price'+menuId).val()
			+'&applicable='+$('#applicable'+menuId).val();
		if(obj.getAttribute('priceid') >= 0){
			url = url+'&priceId='+obj.getAttribute('priceid');
		}
		ajx(url, 'endPrice');
	}

	function endFeature(_htm) {
		if(_htm.trim() != ''){
			document.getElementById(_htm.trim()).checked = !document.getElementById(_htm.trim()).checked;
		}
	}

	function endPrice(_htm) {
		menuId = _htm.trim();
		if(_htm.indexOf('hata') >= 0){
			menuId = _htm.trim().replace('hata','');
		}
		if(_htm.indexOf('hata') >= 0){
			document.getElementById('applicable'+menuId).value = document.getElementById('applicable'+menuId).getAttribute('default');
			document.getElementById('price'+menuId).value = document.getElementById('price'+menuId).getAttribute('default');
		}else{
			document.getElementById('applicable'+menuId).setAttribute('default', document.getElementById('applicable'+menuId).value);
			document.getElementById('price'+menuId).setAttribute('default', document.getElementById('price'+menuId).value);
		}
	}
	var mounthToday = parseInt('<?php echo  date('m', time());?>');
	var dayToday = parseInt('<?php echo  date('d', time());?>');
	var yearToday = parseInt('<?php echo  date('Y', time());?>');
	function dateControl(date){
		dates = date.split('/'); 
		day = parseInt(dates[0]);
		mounth = parseInt(dates[1]);
		year = parseInt(dates[2]);
		if(year >= yearToday){
			if(year == yearToday){
				if(mounth >= mounthToday){
					if(mounth == mounthToday){
						if(day > dayToday){
							return true;
						}else{
							return false;
						}
					}
					return true;
				}else{
					return false;
				}
			}
			return true;
		}else{
			return false;
		}
	}
    </script>
</head>

<body onload="onLoad()">
    <div id="wrapper">
        <div id="inner-page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Yemek Özellikleri (Promosyon, Fiyat, Günün Yemekleri)</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div id="invisContentView" style="display: none; height: 0px;">
            <?php 
            	if(isset($_GET['success'])){
					if($_GET['success'] == 'E'){
						echo '<span style="color:green; font-weight:bolder;">İşlem Başarıyla Gerçekleşti</span>';
					}else{
						echo '<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>';
					}
 				}
            ?>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                        	Yemek Listesi
                        </div>
                        <div class="panel-body">
                        	<?php 
                        	$db = connect();
                        	$stmt ="SELECT PRODUCT_NAME, NOTES, ISPROMOTED, ISFEATURED, PRICE, MENU.ID AS ID 
									FROM MENU LEFT JOIN MENU_FEATURE ON MENU.ID = MENU_FEATURE.MENU_ID, PRICE_LIST
									WHERE PRICE_LIST.MENU_ID = MENU.ID
									AND APPLICABLE_FROM = (SELECT MAX(APPLICABLE_FROM) from PRICE_LIST where MENU_ID = MENU.ID and APPLICABLE_FROM < CURRENT_TIMESTAMP)";
                        	$res = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
                        	$stmt ="SELECT MENU_ID, ID, PRICE, APPLICABLE_FROM FROM PRICE_LIST WHERE APPLICABLE_FROM > CURRENT_TIMESTAMP";
                        	$resPrice = $db->query($stmt) or die($stmt."<br/><br/>".mysql_error());
                        	$priceArray = array();
                        	while($row = $resPrice->fetch_assoc()){
								$priceArray[$row['MENU_ID']] = array('ID' => $row['ID'], 'PRICE' => $row['PRICE'], 'APPLICABLE' => $row['APPLICABLE_FROM']);
							}
                        	?>
                        		<table class="table table-striped table-bordered table-hover" id="dataTables-example">
                        			<thead>
                        	        	<tr>
                        	                <th nowrap="nowrap">Yemek Adı</th>
                        	                <th nowrap="nowrap">Açıklama</th>
                        	                <th nowrap="nowrap">Promosyon</th>
                        	                <th nowrap="nowrap">Günün menüsü</th>
                        	                <th nowrap="nowrap">Aktif Fiyat</th>
                        	                <th nowrap="nowrap">Değişiklik Tarihi</th>
                        	                <th nowrap="nowrap">Yeni Fiyat</th>
                        	                <th style="display: none;">ID</th>
                        	            </tr>
                        			</thead>
                        	        <tbody>
                        	        <?php $odd = true; ?>
                        	       	<?php while($row = $res->fetch_assoc()){?>
                        	        	<tr class="<?php echo $odd ? "odd" : "even";?> " style="cursor: pointer;">
                        		            <td><?php echo $row["PRODUCT_NAME"];?></td>
                        	                <td><?php echo $row["NOTES"];?></td>
                        	                <td><input type="checkbox" onchange="changeFeature(this)" id="promoted<?php echo $row['ID']?>" <?php if($row['ISPROMOTED'] <> null && $row['ISPROMOTED'] == 'E'){echo 'checked="checked"';}?>></td>
                        	                <td><input type="checkbox" onchange="changeFeature(this)" id="featured<?php echo $row['ID']?>" <?php if($row['ISFEATURED'] <> null && $row['ISFEATURED'] == 'E'){echo 'checked="checked"';}?>></td>
                        	                <td style="text-align: right;"><?php echo $row["PRICE"];?></td>
                        	                <?php if($priceArray[$row['ID']] <> null){?>
	                        	                <td><input type="text" class="form-control datepicker" id="applicable<?php echo $row['ID']?>" default="<?php echo date("d/m/Y",strtotime($priceArray[$row['ID']]['APPLICABLE']));?>" priceid="<?php echo $priceArray[$row['ID']]['ID'];?>" value="<?php echo date("d/m/Y",strtotime($priceArray[$row['ID']]['APPLICABLE']))?>"></td>
	                        	                <td><input class="form-control" type="text" onblur="changePrice(this)" onkeypress="validate(event)" id="price<?php echo $row['ID']?>" default="<?php echo $priceArray[$row['ID']]['PRICE']?>" priceid="<?php echo $priceArray[$row['ID']]['ID'];?>" value="<?php echo $priceArray[$row['ID']]['PRICE']?>"></td>
                        	                <?php }else{?>
	                        	                <td><input type="text" class="form-control datepicker" id="applicable<?php echo $row['ID']?>" default="" priceid="-1"></td>
	                        	                <td><input class="form-control" type="text" onblur="changePrice(this)" onkeypress="validate(event)" id="price<?php echo $row['ID']?>" default="" priceid="-1"></td>
                        	                <?php }?>
                        	                <td style="display: none;"><?php echo $row["ID"];?></td>
                        				</tr>
                        				<?php $odd = !$odd;?>
                        			<?php }?>
                        			</tbody>
                        	     </table>
                        		<?php 
                        	$res->close();
                        	disconnect($db);
                        	?>
			            </div>
			       </div>
			   </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    <!-- Page-Level Demo Scripts - Forms - Use for reference -->
</body>
    <script type="text/javascript" src="../js/jquery-ui-1.10.4.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/jquery.dataTables.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>
</html>
<?php ob_end_flush(); ?>